package com.htgd.gardenexpo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConFood;
import com.htgd.gardenexpo.entity.GeConIntegralCategory;
import com.htgd.gardenexpo.entity.GeConIntegralGet;
import com.htgd.gardenexpo.page.IntegralGetQuery;
import com.htgd.gardenexpo.service.GeConIntegralCategoryService;
import com.htgd.gardenexpo.service.GeConIntegralDeductionService;
import com.htgd.gardenexpo.service.GeConIntegralGetService;
import com.htgd.gardenexpo.utils.SecurityParameter;
import com.htgd.gardenexpo.vo.IntegralCategoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 积分获取配置 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-04
 */
@RestController
@RequestMapping("/api/intergralGet")
@Api(tags = "积分获取配置")
@Slf4j
public class GeConIntegralGetController {

    @Autowired
    private GeConIntegralGetService geConIntegralGetService;

    @Autowired
    private GeConIntegralCategoryService geConIntegralCategoryService;

    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public CommonResult<JSONObject> add(@RequestBody GeConIntegralGet geConIntegralGet) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            boolean flag = geConIntegralGetService.getByCategoryIdExist(geConIntegralGet.getCategoryId());
            if(flag){
                result.setCode(300);
                result.setMessage("该类目已存在");
            }else {
                String categoryName = geConIntegralCategoryService.getNameById(geConIntegralGet.getCategoryId());
                geConIntegralGet.setCategoryName(categoryName);
                geConIntegralGet.setStatus("1");
                geConIntegralGetService.save(geConIntegralGet);
                result.setCode(200);
                result.setMessage("添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/edit")
    @ApiOperation("编辑")
    public CommonResult<JSONObject> edit(@RequestBody GeConIntegralGet geConIntegralGet) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            String categoryName = geConIntegralCategoryService.getNameById(geConIntegralGet.getCategoryId());
            geConIntegralGet.setCategoryName(categoryName);
            geConIntegralGetService.updateById(geConIntegralGet);
            result.setCode(200);
            result.setMessage("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    public CommonResult<JSONObject> delete(int id) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConIntegralGetService.removeById(id);
            result.setCode(200);
            result.setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeConIntegralGet>> list(IntegralGetQuery integralGetQuery) {
        CommonResult<List<GeConIntegralGet>> result = new CommonResult<>();
        try {
            PageInfo<GeConIntegralGet> list = geConIntegralGetService.querylist(integralGetQuery);
            result.setCode(200);
            result.setData(list.getList());
            result.setMessage(String.valueOf(list.getTotal()));
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/changeStatus")
    @ApiOperation("更改状态")
    public CommonResult<JSONObject> changeStatus(@RequestBody GeConIntegralGet geConIntegralGet) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConIntegralGetService.changeStatus(geConIntegralGet);
            result.setCode(200);
            result.setMessage("状态更新完成");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/details")
    @ApiOperation("详情")
    @SecurityParameter
    public CommonResult<GeConIntegralGet> details(Integer id){
        CommonResult<GeConIntegralGet> result = new CommonResult<>();
        try{
            GeConIntegralGet geConIntegralGet = geConIntegralGetService.getById(id);
            result.setData(geConIntegralGet);
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }
}
